#!/bin/sh

#  This file is part of Urd.
#
#  Urd is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#  Urd is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program. See the file "COPYING". If it does not
#  exist, see <http://www.gnu.org/licenses/>.
#
# $LastChangedDate: 2008-03-18 17:16:06 +0100 (Tue, 18 Mar 2008) $
# $Rev: 1030 $
# $Author: styck $
# $Id: install.sh 1030 2008-03-18 16:16:06Z styck $
#

#This file is ubuntu specific

uid=$(id -u)

echo "-= URD installer for Ubuntu =-"

if [ "$uid" != "0" ] ; then
	echo "must run as root"
	exit
fi

installer="apt-get"

needed_packages="apache2 php5 mysql-server php5-cli php5-mysql yydecode unrar par2 smarty"
optional_packages="php5-xmlrpc php5-curl arj p7zip-full unace-nonfree trickle unzip tar p7zip "
missing_packages="cksfv"

aptget_options="-qq -y"

php_apache_ini="/etc/php5/apache2/php.ini"
php_cli_ini="/etc/php5/cli/php.ini"

urd_dir_group="www-data"

read -p "Install optional packages [y/n]" si

echo 
# do an upgrade first
$installer $aptget_options update
if [ "$?" != "0" ] ; then
	echo "an error occured; aborting";
	exit $?
fi

# install the required packages
echo "installing $needed_packages"
$installer $aptget_options install $needed_packages
if [ "$?" != "0" ] ; then
	echo "an error occured; aborting";
	exit $?
fi

# if the user said yes, also install the optional packages
if [ "$si" = "y" -o "$si" = "yes" ] ; then
	echo "Installing optional packages $optional_packages"
	$installer $aptget_options install $optional_packages
	if [ "$?" != "0" ] ; then
		echo "an error occured; aborting";
		exit $?
	fi
fi

echo "These are not in the repository: $missing_packages"
echo "if you want to install them, you are on your own"


# run the generic installer to do its thing
echo
echo "Creating urd user and group"
./install.sh

echo
echo "Changing group of the URD directory to $urd_dir_group"
chgrp -R $urd_dir_group . 

echo
echo "Setting directory permissions"
chmod -R g+w .

echo 
echo "checking php memory limit:"
grep "^;*memory_limit" $php_cli_ini
echo "if the limit is below 128M run 'sudo vi $php_cli_ini' and change the setting"

echo 
echo "checking include path in php:"
grep "^;*include_path" $php_apache_ini
echo "is it a comment? Uncomment it and make sure it include '/usr/share/php'. Run 'sudo vi $php_apache_ini'"

echo
echo "you may have to restart apache: sudo /etc/init.d/apache2 restart"


echo 
echo
echo "Install phase 1 complete. Now go to the webbrowser and run phase 2"
echo "eg http://[YOUR IP-ADDRESS OR HOSTNAME]/urd/"

echo
# make sure we end
exit

